<template>
          <div :class="className" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts 主题
// import { debounce } from '@/utils'
import {
    getTemperData
} from '@/api/table'
export default {
  props: {
    className: {
      type: String,
      default: 'chart2'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    selectdate: {
      type: Object
    }
  },
  data() {
    return {
      chart: null,
      Xdata: [],
      Ydata: [],
      tableData: {},
      a: ''
    }
  },
  watch: {
    selectdate: {
      handler() {
        this.setData()
      },
      deep: true
    }
  },
  mounted() {
    this.GetData(this.$route.query.id, 2)
    // setInterval(// , 5000)
    // if (this.autoResize) {
        //   this.__resizeHanlder = (() => {
            //     if (this.chart) {
                //       this.chart.resize()
      //     }
     //   }, 100)
     //   window.addEventListener('resize', this.__resizeHanlder)
      // }
     // this.initChart()
     // 监听侧边栏的变化
    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  },
  beforeDestroy() {
    clearInterval(this.a)
  },
  methods: {
    GetData(id, type) {
      getTemperData(id, type).then((res) => {
        res.content.forEach(element => {
          this.Xdata.push(element.create_time)
          this.Ydata.push(element.temperature)
        })
        this.selectdate.x = this.Xdata
        this.selectdate.y = this.Ydata
        this.initChart()
      })
    },
    setData() {
      var option = {
        title: {
          text: '温度测量曲线图'
        },
        xAxis: {
          data: this.selectdate.x,
          boundaryGap: false
        },
        grid: {
          left: 10,
          top: 30,
          bottom: 20,
          containLabel: true
        },
        visualMap: {
          top: 10,
          right: 0,
          pieces: [{
            gt: 0,
            lte: 45,
            color: '#16c218'
          }, {
            gt: 45,
            lte: 60,
            color: '#ff9224'
          }, {
            gt: 60,
            color: '#f11312'
          }]
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        yAxis: {},
        series: [{
          name: '温度',
          // smooth: true,
          type: 'line',
          // itemStyle: {
          //   normal: {
          //     color: 'rgba(2, 197, 233, 0.2)',
          //     lineStyle: {
          //       color: 'rgba(2, 197, 233, 0.2)'
          //     }
          //   }
          // },
          data: this.selectdate.y,
          animationDuration: 2000,
          animationEasing: 'quadraticOut'
        }]
      }
      this.chart.setOption(option, true)
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setData()
    }
  }
}
</script>
<style>

</style>

